package gameStates 
{
	import entities.*;
	import ggj2014BsAs.grupoJ.datos.LColores;
	import ggj2014BsAs.grupoJ.gui.ContenedorRelojitos;
	import ggj2014BsAs.grupoJ.juego.escena.LevelData;
	import ggj2014BsAs.grupoJ.juego.escena.LLevels;
	import ggj2014BsAs.grupoJ.juego.escena.PlataformaVariable;
	import managers.*;
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;;
	import util.Fx;
	
	public class PlayState extends FlxState
	{
		
		[Embed(source = "../../assets/images/PortalAnim2.png")] public var portalbPNG:Class;
		[Embed(source="../../assets/images/PortalTop.png")]public var portaltPNG:Class;
		[Embed(source = "../../assets/images/mainBg2.png")] public var bgPNG:Class;
		
		[Embed(source = "../../assets/audio/WARP.mp3")]public var portalSnd:Class;
		[Embed(source = "../../assets/audio/robotito-base.mp3")]public var bgSnd:Class;
		
		private var isplyingportal:Boolean = false;
		private var exitPortalB:FlxSprite;
		private var exitPortalT:FlxSprite;
		public  var mapray:Array;
		public var cogs:Vector.<Cog>;
		public var dynamicPlattforms:Vector.<PlataformaVariable> = new Vector.<PlataformaVariable>();
		public var contenedorRelojito:ContenedorRelojitos;
		
		override public function create():void 
		{
			super.create();
			Registry.init();
			
			FlxG.playMusic(bgSnd,1);
			var spriteFondo:FlxSprite = new FlxSprite(0,0,bgPNG);
			add(spriteFondo);
			
			FlxG.bgColor =0xffaaaaaa; 	
			add(Registry.level);
			add(Registry.fx = new Fx());
			
			var levelData:LevelData = LLevels.datosNivelActual;
			
			cogs = levelData.cogs.slice();
			for (var i:int = 0; i < cogs.length; i++) add(cogs[i]);
			
			add(exitPortalB = new FlxSprite(2000, 115).loadGraphic(portalbPNG, true, false, 62, 77, true));
			exitPortalB.addAnimation("flow", [0, 1, 2, 3, 4], 10, true);
			add(Registry.player);
			add(Registry.box = new Box(200, 380));
			add(Registry.hud = new HUD());
			add(exitPortalT = new FlxSprite(2000, 115).loadGraphic(portaltPNG,false,false,62,77,true));
			
			dynamicPlattforms = new Vector.<PlataformaVariable>();
			for (i = 0; i < levelData.platforms.length; i++) dynamicPlattforms.push(levelData.platforms[i]);
			
			FlxG.camera.setBounds(0, 0, 2048, FlxG.height, true);
			FlxG.camera.follow(Registry.player, FlxCamera.STYLE_PLATFORMER);
			
			Registry.hud.startTimer(200);
			Registry.hud.showLevel();
			exitPortalB.play("flow");
			currentColor = 4;
			
			if (contenedorRelojito == null)
			{
				contenedorRelojito = new ContenedorRelojitos();
				GameEngine.agregarContenedorRelojito(contenedorRelojito);
			}
			
			contenedorRelojito.playState = this;
		}
		
		override public function update():void
		{
			super.update();
			
			FlxG.collide(Registry.level, Registry.player);
			FlxG.collide(Registry.level, Registry.box);
			FlxG.collide(Registry.box, Registry.player, Registry.player.isPushing);
			FlxG.overlap( Registry.player,exitPortalT, playSound);
			for (var i:int = 0; i < cogs.length; i++) FlxG.overlap(Registry.player, cogs[i], getColor);
			
			GameEngine.instancia._contenedor.actualizar();
			
			if (FlxG.keys.justPressed("R"))
			{
				//FlxG.resetState();
				FlxG.switchState(new PlayState);
			}
			
			if (FlxG.keys.justPressed("M"))
			{
				FlxG.switchState(new MenuState);		
			}
			
			if (Registry.player.x > 2020)
				finishLevel();
		}
		
		public function playSound(player:Player, portal:FlxSprite):void
		{
			if (!isplyingportal)
			{
			FlxG.play(portalSnd);
			isplyingportal = true;
			}
		}
		
		public function finishLevel():void
		{
			FlxG.flash(0xffc2c2c2, 0.5);
			FlxG.fade(0xff000000, 0.5, onFade);
			FlxG.play(portalSnd);
			Registry.currentLevel++;
			if(Registry.currentLevel <= Registry.finalLevel)
				FlxG.resetState();
			else
				FlxG.switchState(new GameOverState);
		}
		
		public function getColor(player:Player, cog:Cog):void
		{
			cog.runCog();
			var colorAnterior:int = currentColor.valueOf();
			currentColor = cog.col;
			if (cog is TimedCog && colorAnterior != cog.col)
			{
				contenedorRelojito.agregarRelojito(colorAnterior, (cog as TimedCog).time);
			}
			Registry.player.changeColor(cog.col);
		}
		
		public function get currentColor():int { return Registry.currentColor; }
		public function set currentColor(newColor:int):void
		{
			Registry.currentColor = newColor;
			
			var lista:Array = Registry.level.data.slice();
			
			for (var i:int = 0; i < dynamicPlattforms.length; i++) dynamicPlattforms[i].sobreescribirMatriz(lista);
			Registry.level.data = lista;
			Registry.level.updateColors();
		}
		
		private function onFade():void
		{	
		}
	}
}